/*
 * @Author: aggfb 714740235@qq.com
 * @Date: 2024-10-01 10:22:46
 * @LastEditors: aggfb 714740235@qq.com
 * @LastEditTime: 2024-10-16 01:09:44
 * @FilePath: \baking_machine\bsp\bap_timer.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "bsp_timer.h"

static void bsp_timer1_init(void)
{
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_AF);
    rcu_periph_clock_enable(RCU_TIMER1);

    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2);

    timer_deinit(TIMER1);
    timer_initpara.prescaler = (M0_TIMER_PCS - 1U);
    timer_initpara.alignedmode = TIMER_COUNTER_CENTER_BOTH;
    timer_initpara.counterdirection = TIMER_COUNTER_UP;
    timer_initpara.period = (M0_TIMER_PERIOD - 1U);
    timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 1U;
    timer_init(TIMER1, &timer_initpara);

    timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    timer_channel_output_config(TIMER1, TIMER_CH_0, &timer_ocintpara);
    timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocintpara);
    timer_channel_output_config(TIMER1, TIMER_CH_2, &timer_ocintpara);

    timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_0, M0_TIMER_PERIOD / 2);
    timer_channel_output_mode_config(TIMER1, TIMER_CH_0, TIMER_OC_MODE_PWM1);  // TIMER1_CH0 输出比较值 
    timer_channel_output_shadow_config(TIMER1, TIMER_CH_0, TIMER_OC_SHADOW_ENABLE);

    timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, M0_TIMER_PERIOD / 2);
    timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM1); // TIMER1_CH1 输出比较值 
    timer_channel_output_shadow_config(TIMER1, TIMER_CH_1, TIMER_OC_SHADOW_ENABLE);

    timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_2, M0_TIMER_PERIOD / 2);
    timer_channel_output_mode_config(TIMER1, TIMER_CH_2, TIMER_OC_MODE_PWM1); // TIMER1_CH2 输出比较值 
    timer_channel_output_shadow_config(TIMER1, TIMER_CH_2, TIMER_OC_SHADOW_ENABLE);

    timer_primary_output_config(TIMER1, DISABLE);
    timer_auto_reload_shadow_enable(TIMER1);

    timer_master_slave_mode_config(TIMER1, TIMER_MASTER_SLAVE_MODE_ENABLE);
    timer_master_output_trigger_source_select(TIMER1, TIMER_TRI_OUT_SRC_UPDATE);


    TIMER_CNT(TIMER1) = 0;

    timer_channel_output_state_config(TIMER1, TIMER_CH_0, TIMER_CCXN_ENABLE);
    timer_channel_output_state_config(TIMER1, TIMER_CH_1, TIMER_CCXN_ENABLE);
    timer_channel_output_state_config(TIMER1, TIMER_CH_2, TIMER_CCXN_ENABLE);
    // timer_disable(TIMER1);
    timer_enable(TIMER1);
}

static void bsp_timer0_init(void)
{
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_AF);
    rcu_periph_clock_enable(RCU_TIMER0);

    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10);

    timer_deinit(TIMER0);
    timer_initpara.prescaler = (M1_TIMER_PCS - 1U);
    timer_initpara.alignedmode = TIMER_COUNTER_CENTER_BOTH;
    timer_initpara.counterdirection = TIMER_COUNTER_UP;
    timer_initpara.period = (M1_TIMER_PERIOD - 1U);
    timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 1U;
    timer_init(TIMER0, &timer_initpara);

    timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocintpara);
    timer_channel_output_config(TIMER0, TIMER_CH_1, &timer_ocintpara);
    timer_channel_output_config(TIMER0, TIMER_CH_2, &timer_ocintpara);

    timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, M1_TIMER_PERIOD);
    timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM1);  // TIMER1_CH0 输出比较值 
    timer_channel_output_shadow_config(TIMER0, TIMER_CH_0, TIMER_OC_SHADOW_ENABLE);

    timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_1, M1_TIMER_PERIOD);
    timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_PWM1); // TIMER1_CH1 输出比较值 
    timer_channel_output_shadow_config(TIMER0, TIMER_CH_1, TIMER_OC_SHADOW_ENABLE);

    timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_2, M1_TIMER_PERIOD);
    timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_PWM1); // TIMER1_CH2 输出比较值 
    timer_channel_output_shadow_config(TIMER0, TIMER_CH_2, TIMER_OC_SHADOW_ENABLE);

    timer_primary_output_config(TIMER0, DISABLE);
    timer_auto_reload_shadow_enable(TIMER0);

    timer_master_slave_mode_config(TIMER0,TIMER_MASTER_SLAVE_MODE_ENABLE);
    timer_master_output_trigger_source_select(TIMER0,TIMER_TRI_OUT_SRC_UPDATE);

    TIMER_CNT(TIMER0) = 0U;

    // timer_disable(TIMER0);
    timer_enable(TIMER0);
}

static void bsp_timer7_init(void)
{
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_GPIOC);
    rcu_periph_clock_enable(RCU_AF);
    rcu_periph_clock_enable(RCU_TIMER7);

    gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8);

    timer_deinit(TIMER7);
    timer_initpara.prescaler = (M2_TIMER_PCS - 1U);
    timer_initpara.alignedmode = TIMER_COUNTER_CENTER_BOTH;
    timer_initpara.counterdirection = TIMER_COUNTER_UP;
    timer_initpara.period = (M2_TIMER_PERIOD - 1U);
    timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 1U;
    timer_init(TIMER7, &timer_initpara);

    timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    timer_channel_output_config(TIMER7, TIMER_CH_0, &timer_ocintpara);
    timer_channel_output_config(TIMER7, TIMER_CH_1, &timer_ocintpara);
    timer_channel_output_config(TIMER7, TIMER_CH_2, &timer_ocintpara);

    timer_channel_output_pulse_value_config(TIMER7, TIMER_CH_0, M2_TIMER_PERIOD);
    timer_channel_output_mode_config(TIMER7, TIMER_CH_0, TIMER_OC_MODE_PWM1);  // TIMER1_CH0 输出比较值 
    timer_channel_output_shadow_config(TIMER7, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);

    timer_channel_output_pulse_value_config(TIMER7, TIMER_CH_1, M2_TIMER_PERIOD);
    timer_channel_output_mode_config(TIMER7, TIMER_CH_1, TIMER_OC_MODE_PWM1); // TIMER1_CH1 输出比较值 
    timer_channel_output_shadow_config(TIMER7, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);

    timer_channel_output_pulse_value_config(TIMER7, TIMER_CH_2, M2_TIMER_PERIOD);
    timer_channel_output_mode_config(TIMER7, TIMER_CH_2, TIMER_OC_MODE_PWM1); // TIMER1_CH2 输出比较值 
    timer_channel_output_shadow_config(TIMER7, TIMER_CH_2, TIMER_OC_SHADOW_DISABLE);

    timer_primary_output_config(TIMER7, DISABLE);
    timer_auto_reload_shadow_enable(TIMER7);

    timer_master_slave_mode_config(TIMER7,TIMER_MASTER_SLAVE_MODE_ENABLE);
    timer_master_output_trigger_source_select(TIMER7,TIMER_TRI_OUT_SRC_UPDATE);

    TIMER_CNT(TIMER7) = 0;

    timer_disable(TIMER7);
}

static void bsp_timer2_init(void)
{
    timer_parameter_struct timer_initpara;
    rcu_periph_clock_enable(RCU_TIMER2);

    timer_deinit(TIMER2);
    timer_initpara.prescaler = (120U - 1U);
    timer_initpara.alignedmode = TIMER_COUNTER_CENTER_BOTH;
    timer_initpara.counterdirection = TIMER_COUNTER_UP;
    timer_initpara.period = (1000U - 1U);
    timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 1U;
    timer_init(TIMER2, &timer_initpara);

	timer_enable(TIMER2);					

	nvic_irq_enable(TIMER2_IRQn, 5, 1);			  
	timer_interrupt_enable(TIMER2,TIMER_INT_UP);
}

void bsp_timer_init(void)
{
    bsp_timer1_init();
    bsp_timer0_init();
    bsp_timer7_init();
    bsp_timer2_init(); // 1ms 低频数据处理
}
